package com.cheyun.netsalev3.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.MyImageViewBinding
import com.cheyun.netsalev3.utils.GlideRoundTransform

class MyImageView : LinearLayout {
    var activity: AppCompatActivity? = null
    var mContext: Context? = null
    var src = ""
    var binding: MyImageViewBinding? = null
    var radius: Int = 4
    var srcType: Int = 0

    init {
        activity = context as AppCompatActivity
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context
        binding = MyImageViewBinding.inflate(LayoutInflater.from(getContext()), this, true)

        val a = context.obtainStyledAttributes(attrs, R.styleable.MyImageView)
        var r = a.getInt(R.styleable.MyImageView_srcRadius, 4)
        var st = a.getInt(R.styleable.MyImageView_srcType, 0)
        this.radius = r
        this.srcType = st
        if (st == 1 || st == 2) {
            val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
            params.setMargins(5, 5, 5, 5)
            binding?.myImgLl?.layoutParams = params

            val lp = binding?.myImg?.getLayoutParams()!!
            lp.width = LayoutParams.MATCH_PARENT
            lp.height = LayoutParams.MATCH_PARENT
            binding?.myImg?.layoutParams = lp
        }
    }


    fun setImage(img: String) {
        if (srcType == 0) {
            if (img.isEmpty() || !img.startsWith("http", true)) {
                return
            }
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
            Glide.with(this.context!!).load(img).transform(GlideRoundTransform(this.context, radius)).placeholder(R.mipmap.logo).into(binding?.myImg!!)
        } else if (srcType == 1) {
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
//            Glide.with(this.context!!).load(img).transform(GlideRoundTransform(this.context, radius)).placeholder(R.drawable.morentouxiang_tu).into(binding?.myImg!!)
        } else if (srcType == 2) {
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
//            Glide.with(this.context!!).load(img).transform(GlideRoundTransform(this.context, radius)).placeholder(R.drawable.shangpintupian_tu).into(binding?.myImg!!)
        } else if(srcType == 3){
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
            binding?.myImg!!.setImageResource(img.toInt())
        } else if(srcType == 4){
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
//            if(img.equals("0")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_chengse)
//            }else if(img.equals("1")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_zise)
//            }else if(img.equals("2")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_tianlanse)
//            }else if(img.equals("3")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_danlvse)
//            }else if(img.equals("4")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_chengse)
//            }else if(img.equals("5")){
//                binding?.myImg!!.setBackgroundResource(R.drawable.rectangle_6dp_zise)
//            }
            binding?.myImg!!.setImageResource(R.drawable.shijiazhong)
//            binding?.myImg!!.setImageResource(R.drawable.rectangle_6dp_chengse)
        }else if(srcType == 5){
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
            Glide.with(this.context!!)
                    .load(img)
                    .transform(GlideRoundTransform(this.context, radius))
                    .placeholder(R.drawable.datuchicun)
                    .error(R.drawable.datuchicun)
                    .into(binding?.myImg!!)
        }else if(srcType == 6){
            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
            if(img.isEmpty()){
                Glide.with(this.context!!)
                        .load(R.drawable.datuchicun)
                        .transform(GlideRoundTransform(this.context, radius))
                        .into(binding?.myImg!!)
            }else{
                var imgs = img.split(",")
                if(imgs.size == 0){
                    Glide.with(this.context!!)
                            .load(R.drawable.datuchicun)
                            .transform(GlideRoundTransform(this.context, radius))
                            .into(binding?.myImg!!)
                }else{
                    Glide.with(this.context!!)
                            .load(imgs.get(0))
                            .transform(GlideRoundTransform(this.context, radius))
                            .placeholder(R.drawable.datuchicun)
                            .error(R.drawable.datuchicun)
                            .into(binding?.myImg!!)
                }
            }
        }else if(srcType == 7){
//            binding?.myImg?.scaleType = ImageView.ScaleType.FIT_XY
//            if(img.isEmpty()){
//                Glide.with(this.context!!)
//                        .load(R.drawable.baimingdantouxiang)
//                        .transform(GlideRoundTransform(this.context, radius))
//                        .into(binding?.myImg!!)
//            }else{
//                Glide.with(this.context!!)
//                        .load(img)
//                        .transform(GlideRoundTransform(this.context, radius))
//                        .placeholder(R.drawable.baimingdantouxiang)
//                        .error(R.drawable.baimingdantouxiang)
//                        .into(binding?.myImg!!)
//            }
        }else{
            Glide.with(this.context!!).load(img).into(binding?.myImg!!)
        }
    }

    fun setImageRound(radius: String) {
        this.radius = radius.toInt()
    }
}